package cn.iocoder.yudao.module.wine.service.businessuserlog;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.businessuserlog.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuserlog.BusinessUserLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.businessuserlog.BusinessUserLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 商家认证日志 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BusinessUserLogServiceImpl implements BusinessUserLogService {

    @Resource
    private BusinessUserLogMapper businessUserLogMapper;

    @Override
    public Long createBusinessUserLog(BusinessUserLogSaveReqVO createReqVO) {
        // 插入
        BusinessUserLogDO businessUserLog = BeanUtils.toBean(createReqVO, BusinessUserLogDO.class);
        businessUserLogMapper.insert(businessUserLog);
        // 返回
        return businessUserLog.getId();
    }

    @Override
    public void updateBusinessUserLog(BusinessUserLogSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessUserLogExists(updateReqVO.getId());
        // 更新
        BusinessUserLogDO updateObj = BeanUtils.toBean(updateReqVO, BusinessUserLogDO.class);
        businessUserLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessUserLog(Long id) {
        // 校验存在
        validateBusinessUserLogExists(id);
        // 删除
        businessUserLogMapper.deleteById(id);
    }

    private void validateBusinessUserLogExists(Long id) {
        if (businessUserLogMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public BusinessUserLogDO getBusinessUserLog(Long id) {
        return businessUserLogMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessUserLogDO> getBusinessUserLogPage(BusinessUserLogPageReqVO pageReqVO) {
        return businessUserLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BusinessUserLogDO> appGetBusinessUserLogList() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return businessUserLogMapper.selectList(new LambdaQueryWrapperX<BusinessUserLogDO>()
                .eqIfPresent(BusinessUserLogDO::getAppUserId,loginUserId)
                .orderByDesc(BusinessUserLogDO::getCreateTime));

    }

}
